package com.liuyuncen.repository;

import com.liuyuncen.entity.Feature;
import com.liuyuncen.entity.Person;
import com.liuyuncen.entity.Svo;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;

/**
 * @Description SvoRepository
 * @Author xhl
 * @Date 2023/7/10 14:44
 **/
public interface SvoRepository extends Neo4jRepository<Svo, Long> {

    /**
     * 根据svo code
     * @param code
     * @return 特征清单
     */
    @Query("match p=(n:Svo)-[r:svo_feature]->(n1:Feature) where n.name=$code  return n1")
    List<Feature> findFeatureByCode(@Param("code") String code);

    /**
     * 全部svo数据
     */
    @Query("match (svo:Svo) return svo")
    List<Svo> getAllSvo();

    @Query("match (svo:Svo) return svo.name")
    List<String> getAllSvoCode();

    @Query("match (svo:Svo) return svo")
    void createTestSvo();

    @Query("MATCH (u1:Svo{name: 'base'}),(u2:Svo{name:$sapCode}) WITH u1, u2 OPTIONAL MATCH common=(u1)-[:svo_feature]->(t:Feature)<-[:svo_feature]-(u2) return COUNT(common)")
    int queryCommonFeatureNum(@Param("sapCode") String sapCode);

    @Query("MATCH (startNode:Svo {name: 'base'}), (endNode:Svo)\n" +
            "WHERE startNode <> endNode\n" +
            "MATCH path = shortestPath((startNode)-[*]-(endNode))\n" +
            "RETURN endNode,length(path)\n" +
            "ORDER BY length(path)\n" +
            "LIMIT 3")
    Object findShortest();

    //查找与base节点最相似的5个节点（特征最相近）
    @Query("MATCH (u1:Svo{name: 'base'}), (u2:Svo)\n" +
            "where u1 <> u2\n" +
            "WITH u1, u2\n" +
            "OPTIONAL MATCH common=(u1)-[:svo_feature]->(t:Feature)<-[:svo_feature]-(u2)\n" +
            "return COUNT(common) as commonSize ,u2.name as sapCode\n" +
            "order by COUNT(common) desc limit $recommendSize")
    List<Map<String,Object>> queryTopSimilarNode(@Param("recommendSize") int recommendSize);


    @Query("match (a:Svo{name:'base'})-[r:svo_feature]->(b) delete r ")
    void deleteBaseNodeRelationship();

    @Query("match (a:Svo{name:'base'}) delete a")
    void deleteBaseNode();

    @Query("match (a:Svo) where a.name in $sapStr with a match (a)-[r:svo_order]->(order) return a.name as sapCode,order.name as orderCode")
    List<Map<String,String>> queryOrderMap(@Param("sapStr") List<String> sapStr);

}
